use std::sync::Arc;
use async_trait::async_trait;
use sea_orm::{DatabaseConnection, EntityTrait};
use crate::entity::cld_indexcondition::Model;
use crate::entity::prelude::CldIndexcondition;
use crate::repo::point_repo::DbContextHolder;

pub struct XunYouRepo {
    db_contexts: Arc<Vec<DatabaseConnection>>,
}

impl DbContextHolder for XunYouRepo {
    fn get_db_contexts(&self) -> &Vec<DatabaseConnection> {
        &self.db_contexts
    }
}

impl XunYouRepo {
    pub fn new(dbs: Arc<Vec<DatabaseConnection>>) -> Self {
        Self { db_contexts: dbs }
    }

    pub async fn get_all_working_conditions_async(&self, machine_index: u32) -> Result<Vec<Model>, String> {
        let db_context = self.get_db_context(machine_index);
        let result = CldIndexcondition::find().all(db_context).await;
        let models = match result {
            Ok(models) => models,
            Err(err) => return Err(format!("数据库错误: {}", err)),
        };
        Ok(models)
    }
}

